#!/usr/bin/env bash
#
# Copyright (C) 2000-2025 Kern Sibbald
# Copyright (C) 2021-2023Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run cppcheck
#

TARGET=$1

if [ "$TARGET" == ALL ]; then
    cppcheck --quiet --enable=all --std=c++11 --force --suppress=unreadVariable --suppress=cstyleCast --language=c++ --suppress=unusedFunction ../bacula/src
    exit $?
fi

if [ "$TARGET" != "" ]; then
    ret=0
    cd ..
    git diff-tree --no-commit-id --name-only -r ${TARGET}..HEAD > /tmp/file.list.$$
    if grep -E c$ /tmp/file.list.$$ > /dev/null
    then
        grep -E '[ch]$' /tmp/file.list.$$ | xargs cppcheck --quiet --enable=all --std=c++11 --force --suppress=unreadVariable --suppress=unusedFunction --suppress=cstyleCast --language=c++
        ret=$(($ret + $?))
    fi
    while read file; do
        if file $file | grep Python > /dev/null ; then
            pylint $file
            ret=$(($ret + $?))
        fi
    done < /tmp/file.list.$$
    exit $ret
fi
